package com.ddf.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 在xml文件中配置到Spring的上下文中
 */
public class AllInterceptor implements HandlerInterceptor{
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private RequestContext requestContext;
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj2, Exception exception)
			throws Exception {
		log.info("[AllInterceptor]  afterCompletion");
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mv)
			throws Exception {
		log.info("[AllInterceptor]  postHandle");
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		log.info("[AllInterceptor]  preHandle");
		Enumeration<String> params = request.getParameterNames();
		while(params.hasMoreElements()) {
			String key = params.nextElement();
			log.info(key + ":" + request.getParameter(key));
			requestContext.put(key, request.getParameter(key));
		}
		return true;
	}
}
